package com.smartTerminal.perMatters.service.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.smartTerminal.perMatters.service.MyAppointmentService;
import com.smartTerminal.webservice.TerminalServices;

/**
 * 处理预约接口的实现
 * 
 * @author wantao
 * @Date 2018年1月9日
 * 
 */
@Service
public class MyAppointmentServiceImpl implements MyAppointmentService {

	/**
	 * 根据事项id和预约日期获取对应日期可以预约的时间段
	 * @param itemid 根据事项id
	 * @param time 预约日期
	 * @return 时间段
	 */
	public String getItemAppointmentTime(String itemid, String time) throws Exception {
		if(itemid==null){
			throw new Exception("事项id不能为空");
		}
		if(time==null){
			throw new Exception("时间不能为空");
		}
		String data = TerminalServices.getInstance().getTerminalServicesHttpPort().getItemAppointmentTime(itemid, time);
		return data;
	}

	/**
	 * 校验
	 * @param userName 预约人
	 * @param phone 手机号
	 * @param idcard 身份证
	 * @return error
	 */
	public String checkError(String userName,String phone,String idcard){
		String error = null;
		String phoneRegex = "/^1[3|4|5|7|8][0-9]{9}$/";
		String idCardRegexp = "/(^/d{15}$)|(^/d{18}$)|(^/d{17}(/d|X|x)$)/";
		if(userName == null || userName == ""){
			error = "请填写预约人姓名！";
		}
		if(phone == null || phone == ""){
			error = "请填写预约电话！";
		}
		if(phone.length()>11 || phone.matches(phoneRegex)){
			error  = "手机号码格式有误，请检查手机号码";
		}
		if(idcard == null || idcard == ""){
			error = "请填写预约人身份证号码！";
		}
		if(idcard.matches(idCardRegexp)){
			error  = "身份证号码格式有误，请检查身份证号码";
		}
		
		return error;
	}
	
	/**
	 * 保存预约信息
	 * @param token 登录凭证
	 * @param xmlInfo 预约信息
	 * @return
	 * @throws Exception
	 */
	public String saveAppointmentInfo(String token,String xmlInfo) throws Exception{

		String data = TerminalServices.getInstance().getTerminalServicesHttpPort().saveAppointmentInfo(token,xmlInfo);

		JSONObject obj = JSONObject.parseObject(data);
		String ORDER_NO = obj.getString("ORDER_NO");
		
		if(data!=null){
			if(!obj.getString("code").equals("0") || obj.getString("code").equals("-1")){
				return obj.getString("code");
			}
		}
		return ORDER_NO;
	}

	/**
	 * 
	 * @param token 登录凭证
	 * @param itemname 事项名称用于模糊查询，可为空
	 * @param currentpage 分页信息：当前第几页
	 * @param pagesize 分页信息：当前页几条数据
	 * @return 
	 */
	public String getMyAppointmentList(String token,String orderNo,String itemname,
			String currentpage, String pagesize) throws Exception{
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getMyAppointmentList(token, "", "", currentpage, pagesize);
	}

	/**
	 * 获取预约详情
	 * @param aId id预约编号
	 * @return
	 */
	public String getMyAppointmentById(String aId) {
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getMyAppointmentById(aId);
	}

}
